/**
 * Created by zhangyi on 2017/11/9.
 */
mainApp.factory("dayclearFactory",function(){
    var service={};

    //修改时获取数据
    var object;
    service.setDayclear=function(data){
        object=data;

    }
    service.getDayclear=function(){
        return object;
    }

    //获取全部列表
    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list = "";
        RequestUtils.postJson('../sumday/getNow',obj).done(function (resp) {
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });
        return list;
    };

    //新增日清数据的service
    service.addDayclear=function(dayclearpObj){
        if(dayclearpObj.date!="" && dayclearpObj.date!=null) {
            RequestUtils.postForm('../sumday/add', dayclearpObj).done(function (resp) {
                if (resp.resultCode == 0) {
                    CommonUtils.buildSuccessHint('add', '成功');
                } else {
                    CommonUtils.buildFailHint('add', '失败');
                }
            });
        }
    }

    //修改日清数据的service
    service.UpdateDayclear=function(dayclears){
        RequestUtils.postJson('../sumday/update', dayclears)
            .done(function (resp) {
                console.log(resp);
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint('update', '成功');
                }else{
                    CommonUtils.buildFailHint('update', '失败');
                }
            });
    }

    //根据工号(id)查询日清记录service
    service.selectDayclearById=function(id){
        var list="";
        RequestUtils.getJson("../sumday/getByIdInweek/"+id).done(function(resp){
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint('记录查询', '成功');
            }else{
                CommonUtils.buildFailHint('记录查询', '失败');
            }
        });
        return list;
    }

    //删除日清记录service
    service.delete=function(id){
        RequestUtils.getJson("../sumday/delete/"+id).done(function(resp){
            if(resp.resultCode==0){
                CommonUtils.buildSuccessHint('delete', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('delete', '失败');
            }
        })
    };

    //按日期区间和工号查询日清记录service
    service.selectDayclearBynumAndDate=function(dayclObj,currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 7
        }
        if(dayclObj.jobnum=="" || dayclObj.jobnum==null){
            dayclObj.jobnum="null";
        }
        if(dayclObj.startDate=="" || dayclObj.startDate==null){
            dayclObj.startDate="null";
        }
        if(dayclObj.endDate=="" || dayclObj.endDate==null){
            dayclObj.endDate="null";
        }
        var list="";
        RequestUtils.postJson("../sumday/getByJobnumTime/"+dayclObj.jobnum+"/"+dayclObj.startDate+"/"+dayclObj.endDate,obj)
            .done(function(resp){
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint('记录查询', '成功');
                }else{
                    CommonUtils.buildFailHint('记录查询', '失败');
                }
            })
        dayclObj.jobnum="";
        dayclObj.startDate="";
        dayclObj.endDate="";
        return list;
    };

    return service;
});

mainApp.controller("dayListController",function($scope,$http,$state,dayclearFactory){
    //获取数据 全部的
    $scope.dayclearList=dayclearFactory.getListData(1);
    //获取list
    $scope.list=$scope.dayclearList.items;

    //查询工号，跳转修改页面
    $scope.selectDayclear=function(id){
        //添加标签
        var tab={url:"dayclear-update",text:"日清详情",id:"dayclear-update-tab",closeable:1};
        $scope.$emit('addTab', tab);

        var daycl=dayclearFactory.selectDayclearById(id);
        dayclearFactory.setDayclear(daycl);
        $state.go('dayclear-update');
    }

    //删除日清记录
    $scope.delete=function(id){
        dayclearFactory.delete(id);
    };

    $scope.listShow=true;
    $scope.listByWhereShow=false;

    //工号和时间区间条件查询
    $scope.dayclearWhere;
    $scope.selectWhere=function(dayclearWhereObj){
        $scope.listShow=false;
        $scope.listByWhereShow=true;
        $scope.dayclearList=dayclearFactory.selectDayclearBynumAndDate(dayclearWhereObj,1);
        $scope.list=$scope.dayclearList.items;
    }

    $scope.addTab=function(){
        var tab={url:"dayclear-add",text:"日清新增",id:"dayclear-add-tab",closeable:1};
        $scope.$emit('addTab', tab);
    };

    //翻页
    $scope.NextListData=function(pageIndex){
        if($scope.listByWhereShow){
            $scope.dayclearList=dayclearFactory.selectDayclearBynumAndDate($scope.dayclearWhere,pageIndex);
        }else{
            $scope.dayclearList=dayclearFactory.getListData(pageIndex);
        }
        $scope.list=$scope.dayclearList.items;
    }

    CommonUtils.setDatetimepicker("startDatetimepicker");
    CommonUtils.setDatetimepicker("endDatetimepicker");
});

mainApp.controller("dayAddController",function($scope,$http,dayclearFactory,$cookieStore,overallSituationService) {
    overallSituationService.loginIntercept();

    //新增日清记录
    $scope.addDayclear=function(dayclear){
        var dayclearObj={
            "date":dayclear.date,
            "task":dayclear.task,
            "result":dayclear.result,
            "incomplete_rate":dayclear.incomplete_rate,
            "remark":dayclear.remark,
            "employee.id":1
        };
        console.log(dayclearObj);
        dayclearFactory.addDayclear(dayclearObj);
    };

    //日清结果下拉框
    $scope.results=[
        {name:"处理中",value:"0"},
        {name:"已完成",value:"1"}
    ];

    $scope.LoginEmp = $cookieStore.get('LoginEmp');
    console.log($scope.LoginEmp);

    CommonUtils.setDatetimepicker("datetimepicker");
});

mainApp.controller("dayUpdateController",function($scope,$http,dayclearFactory){
    //修改时查询出的数据列表
    $scope.dayclears=dayclearFactory.getDayclear();
    //日期的计算
    var date=CommonUtils.calculateDate($scope.dayclears.date);
    $scope.startDay=date[0];
    $scope.endDay=date[1];
    $scope.weekDay=date[2];

    //日清结果下拉框
    $scope.results=[
        {name:"处理中",value:"0"},
        {name:"已完成",value:"1"}
    ];

    //修改后的提交
    $scope.Update=function(dayclearObjs){
        var dayclears=[];
        for (var i = 0; i < dayclearObjs.length; i++) {
            var sumday={
                id:dayclearObjs[i].id,
                date:dayclearObjs[i].date,
                task:dayclearObjs[i].task,
                result:dayclearObjs[i].result,
                incomplete_rate:dayclearObjs[i].incomplete_rate,
                week_rate:dayclearObjs[i].week_rate,
                remark:dayclearObjs[i].remark,
                employee:dayclearObjs[i].employee
            }
            dayclears.push(sumday);
        }
        dayclearFactory.UpdateDayclear(dayclears);
    };
});

